from selenium import webdriver
from selenium.webdriver.common.by import By
from common.basepage import Basepage
from common.yaml_handler import result

Host = result['Host']


class LoginPage(Basepage):
    url = Host + '#/login'

    # 存放元素定位属性
    # 用户名
    username_locator = (By.XPATH, "//input[@name='username']")
    # 密码
    password_locator = (By.XPATH, "//input[@name='password']")
    # 登录按钮
    login_locator = (By.XPATH, "//button[@class='el-button handlebutton el-button--primary el-button--medium']")
    # 错误信息
    error_locator = (By.XPATH, "//div[@class='el-form-item__error']")
    # 成功信息弹窗
    succsee_window_locator = (By.XPATH, "//div[@class='el-message el-message--success']")
    # 失败信息弹窗
    erroor_window_locator = (By.XPATH, "//div[@class='el-message el-message--waring']")

    def login(self, username, password):
        """登录过程"""
         # 输入用户名及密码
        self.driver.find_element(*self.username_locator).send_keys(username)
        self.driver.find_element(*self.password_locator).send_keys(password)
        self.click(self.login_locator)
        return self

    def load(self,):
        """访问页面"""
        self.driver.get(self.url)
        return self

    def get_error_msgs(self):
        """获取错误信息"""
        elem = self.driver.find_element(*self.error_locator)
        text = elem.text
        return text

    def get_error_window_msgs(self):
        """获取弹窗的错误信息"""
        elem = self.driver.find_element(*self.erroor_window_locator)
        text = elem.text
        return text

    def get_success_window_msgs(self):
        """获取登录成功的弹窗信息"""
        elem = self.driver.find_element(*self.succsee_window_locator)
        text = elem.text
        return text
